import LogicSolver from "./logicSolver";
import TracedownSolver from "./tracedownSolver";

export default class Solver {
  private puzzle: number[];
  constructor(puzzle: number[]) {
    this.puzzle = puzzle;
  }

  public test() {
    console.time("Pure Tracedown");
    for (let i = 0; i < 1000; i++) {
      const solver = new TracedownSolver([...this.puzzle]);
      solver.solve();
    }
    console.timeEnd("Pure Tracedown");

    console.time("TraceDown + Logic");
    for (let i = 0; i < 1000; i++) {
      const logicSolver = new LogicSolver([...this.puzzle]);
      logicSolver.solve();
      const traceDownSolver = new TracedownSolver([...logicSolver.table]);
      traceDownSolver.solve();
    }
    console.timeEnd("TraceDown + Logic");
  }

  public solve(): number[] {
    const logicSolver = new LogicSolver([...this.puzzle]);
    if (logicSolver.solve()) {
      this.puzzle = logicSolver.table;
      return this.puzzle;
    }
    const traceDownSolver = new TracedownSolver([...logicSolver.table]);
    traceDownSolver.solve();
    this.puzzle = traceDownSolver.table;
    return this.puzzle;
  }

  public get table(): number[] {
    return this.puzzle;
  }

  public showAnswer() {
    for (let i = 0; i < 9; i++) {
      console.log(this.puzzle.slice(i * 9, (i + 1) * 9).join(","));
    }
  }
}
